package com.mybatisplus.springboot;

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor;
import com.mybatisplus.springboot.handler.MyMetaObjectHandler;
import com.mybatisplus.springboot.injectors.MySqlInjector;
import com.mybatisplus.springboot.plugins.MyInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
@MapperScan("com.mybatisplus.springboot.mapper")  //扫描mapper接口包
public class MybatisPlusConfig {

    @Bean //配置分页插件
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }

    @Bean //注入自定义的拦截器(插件)
    public MyInterceptor myInterceptor() {
        return new MyInterceptor();
    }

    @Bean //sql分析插件
    public SqlExplainInterceptor sqlExplainInterceptor() {

        SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();

        List<ISqlParser> list = new ArrayList<>();

        list.add(new BlockAttackSqlParser());  //全表更新、删除的阻断器

        sqlExplainInterceptor.setSqlParserList(list);

        return sqlExplainInterceptor;
    }

    @Bean //性能分析插件
    public PerformanceInterceptor performanceInterceptor() {
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();

        //执行时间
        performanceInterceptor.setMaxTime(100);
        //格式化
        performanceInterceptor.setFormat(true);

        return performanceInterceptor;
    }

    @Bean //乐观锁
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }

    @Bean //注入自定义的SQL注入器
    public MySqlInjector mySqlInjector() {
        return new MySqlInjector();
    }

    @Bean //自动填充
    public MyMetaObjectHandler myMetaObjectHandler() {
        return new MyMetaObjectHandler();
    }
}


